package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.Building;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.entity.DormitoryAdmin;
import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.mapper.BuildingMapper;
import com.pzj.dormitory.mapper.DormitoryAdminMapper;
import com.pzj.dormitory.mapper.DormitoryMapper;
import com.pzj.dormitory.mapper.StudentMapper;
import com.pzj.dormitory.service.BuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.vo.BuildingVO;
import com.pzj.dormitory.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public PageVO list(Integer page, Integer pageSize) {
        Page<Building> pageInfo = new Page<>(page, pageSize);
        Page<Building> buildingPage = buildingMapper.selectPage(pageInfo, null);
        List<Building> buildings = buildingPage.getRecords();
        ArrayList<BuildingVO> buildingVOS = new ArrayList<>();

        // VO转换
        buildings.forEach(building ->{
            BuildingVO buildingVO = new BuildingVO();
            BeanUtils.copyProperties(building, buildingVO);
            DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectById(building.getAdminId());
            buildingVO.setAdminName(dormitoryAdmin.getName());
            buildingVOS.add(buildingVO);
        });
        PageVO pageVO = new PageVO();
        pageVO.setTotal(buildingPage.getTotal());
        pageVO.setData(buildingVOS);

        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        // 模糊查询加分页
        Page<Building> objectPage = new Page<>(searchForm.getPage(),searchForm.getSize());
        // 新建page对象
        Page<Building> page = null;
        // 判断查询字段是否为空
        if(searchForm.getValue().equals("")){
            page = buildingMapper.selectPage(objectPage,null);
        }else {
            QueryWrapper<Building> buildingQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.like(searchForm.getKey(),searchForm.getValue());
            page = buildingMapper.selectPage(objectPage,buildingQueryWrapper);
        }
        List<Building> buildings = page.getRecords();

        List<BuildingVO> buildingVOS = new ArrayList<>();
        // VO转换
        buildings.forEach(building ->{
            BuildingVO buildingVO = new BuildingVO();
            BeanUtils.copyProperties(building,buildingVO);
            DormitoryAdmin admin = dormitoryAdminMapper.selectById(building.getAdminId());
            buildingVO.setAdminName(admin.getName());
            buildingVOS.add(buildingVO);
        });
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setData(buildingVOS);
        return pageVO;
    }

    @Override
    public boolean deleteById(Integer id) {
        // 找到楼宇中的所有宿舍
        // 找到宿舍中所有学生
        // 给学生分配宿舍
        // 删除宿舍
        // 删除楼宇
        QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
        dormitoryQueryWrapper.eq("building_id",id);
        List<Dormitory> dormitories = dormitoryMapper.selectList(dormitoryQueryWrapper);
        for (Dormitory dormitory : dormitories){
            // 根据宿舍号，查找学生
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.like("dormitory_id",dormitory.getId());
            List<Student> studentList = studentMapper.selectList(studentQueryWrapper);

            // 遍历学生集合，给每个学生换宿舍
            for (Student student : studentList){
                // 找到空闲宿舍
                Integer availableDorId = dormitoryMapper.findAvailableDorId();
                student.setDormitoryId(availableDorId);
                try {
                    studentMapper.updateById(student);
                    // 空闲床位-1
                    dormitoryMapper.subAvailableById(availableDorId);
                } catch (Exception e) {
                   return false;
                }
            }
            // 删除宿舍
            int i = dormitoryMapper.deleteById(dormitory.getId());
            if (i !=1)return false;
        }
        // 删除楼宇
        int i = buildingMapper.deleteById(id);
        if (i !=1)return false;
        return true;
    }
}
